package edu.tjut.tjutcommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.tjut.tjutcommunity.entity.*;
import edu.tjut.tjutcommunity.mapper.CommentMapper;
import edu.tjut.tjutcommunity.service.CommentService;
import edu.tjut.tjutcommunity.service.FloorService;
import edu.tjut.tjutcommunity.service.PostService;
import edu.tjut.tjutcommunity.service.UserService;
import edu.tjut.tjutcommunity.utils.MessageQueueUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhh
 * @since 2022-04-18
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    final
    CommentMapper commentMapper;
    final
    FloorService floorService;
    final
    PostService postService;
    final
    MessageQueueUtils messageQueueUtils;
    final
    UserService userService;

    public CommentServiceImpl(CommentMapper commentMapper, FloorService floorService, PostService postService, MessageQueueUtils messageQueueUtils, UserService userService) {
        this.commentMapper = commentMapper;
        this.floorService = floorService;
        this.postService = postService;
        this.messageQueueUtils = messageQueueUtils;
        this.userService = userService;
    }

    /**
     * 获取楼中楼评论
     *
     * @param fid
     * @param pageNo
     * @return
     */
    @Override
    public PageResult<Comment> getComment(Integer fid, Integer pageNo) {
        Page<Comment> page = new Page<>(pageNo, 10);
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("fid", fid);
        qw.orderByAsc("create_time");
        Page<Comment> commentPage = commentMapper.selectPage(page, qw);
        return new PageResult<>(commentPage.getRecords(), commentPage.getPages());
    }

    /**
     * 发送楼中楼评论并通知层主
     *
     * @param comment
     * @param user
     * @param fid
     * @return
     */
    @Override
    public boolean sendComment(Comment comment, User user, Integer fid) {
        comment.setUid(user.getUid());
        comment.setUsername(user.getUsername());
        comment.setPid(floorService.getPidByFid(fid));
        if (commentMapper.insert(comment) == 1) {
            //应该在这里通知
            //需要获取楼层的uid
            Floor floor = floorService.getUidAndUsernameByFid(fid);
            if (!Objects.equals(floor.getUid(), user.getUid())) {
                //获取stuid
                User u = userService.getStuIdById(floor.getUid());
                NoticeMessage noticeMessage = NoticeMessage.getReplyFloorMessage(user, fid, comment.getPid(), comment.getContent());
                //通知
                messageQueueUtils.sendMessage(u.getStudentId(), noticeMessage);
            }
            //帖子更新
            if (!postService.updateTime(comment.getPid())) return false;
            //楼层评论数加一
            if (!floorService.commentsInc(fid)) return false;
            return true;
        }
        return false;
    }

    /**
     * 获取该层下的评论数量
     *
     * @param fid
     * @return
     */
    @Override
    public Integer selectCountByFid(Integer fid) {

        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("fid", fid);
        return Math.toIntExact(commentMapper.selectCount(qw));
    }

    @Override
    public List<Comment> getAllComment(Integer fid) {
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("fid", fid);
        qw.orderByAsc("create_time");
        return commentMapper.selectList(qw);

    }
}
